package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.PersonPhysicalExaminationArchivesMapper;
import com.zhijian.medical.entity.po.Person;
import com.zhijian.medical.entity.po.PersonPhysicalExaminationArchives;
import com.zhijian.medical.entity.vo.request.PersonPhysicalExaminationArchivesSearchVo;
import com.zhijian.medical.entity.vo.response.PersonDetailVo;
import com.zhijian.medical.entity.vo.response.PersonPhysicalExaminationArchivesResultVo;
import com.zhijian.medical.entity.vo.response.PersonPhysicalExaminationArchivesVo;
import com.zhijian.medical.enums.ResponseCodeEnum;
import com.zhijian.medical.exception.BusinessException;
import com.zhijian.medical.service.PersonDetailService;
import com.zhijian.medical.service.PersonPhysicalExaminationArchivesService;
import com.zhijian.medical.service.PersonService;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 人员-体检档案卡业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-03-25 16:44:11
 */
@Slf4j
@Service
public class PersonPhysicalExaminationArchivesServiceImpl implements PersonPhysicalExaminationArchivesService {

    @Autowired
    private PersonPhysicalExaminationArchivesMapper mapper;

    @Autowired
    private PersonService personService;

    @Autowired
    private PersonDetailService personDetailService;

    @Override
    public PersonPhysicalExaminationArchives selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PersonPhysicalExaminationArchivesVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(PersonPhysicalExaminationArchives personPhysicalExaminationArchives) {
        Person person = personService.selectById(personPhysicalExaminationArchives.getPersonId());
        if (Objects.isNull(person)) {
            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "此用户不存在或者已被删除");
        }
        Date now = new Date();
        personPhysicalExaminationArchives.setCreateTime(now);
        personPhysicalExaminationArchives.setUpdateTime(now);
        return mapper.insert(personPhysicalExaminationArchives) > 0 ? personPhysicalExaminationArchives.getId() : 0L;
    }

    @Override
    public boolean update(PersonPhysicalExaminationArchives personPhysicalExaminationArchives) {
        Date now = new Date();
        personPhysicalExaminationArchives.setUpdateTime(now);
        return mapper.updateByPrimaryKey(personPhysicalExaminationArchives) != 0;
    }

    @Override
    public Integer count(PersonPhysicalExaminationArchivesSearchVo personPhysicalExaminationArchivesSearchVo) {
        return mapper.count(personPhysicalExaminationArchivesSearchVo);
    }

    @Override
    public List<PersonPhysicalExaminationArchivesVo> selectList(PersonPhysicalExaminationArchivesSearchVo personPhysicalExaminationArchivesSearchVo) {
        return mapper.selectList(personPhysicalExaminationArchivesSearchVo);
    }

    @Override
    public Page<PersonPhysicalExaminationArchivesVo> selectPage(PersonPhysicalExaminationArchivesSearchVo personPhysicalExaminationArchivesSearchVo) {
        //先直接查总数
        int count = count(personPhysicalExaminationArchivesSearchVo);
        //分页
        Page<PersonPhysicalExaminationArchivesVo> page = new Page<>(true, count, personPhysicalExaminationArchivesSearchVo.getPageNum(), personPhysicalExaminationArchivesSearchVo.getPageSize());
        personPhysicalExaminationArchivesSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PersonPhysicalExaminationArchivesVo> responseVos = this.selectList(personPhysicalExaminationArchivesSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public PersonPhysicalExaminationArchivesResultVo selectPersonPhysicalExaminationArchivesResultVo(PersonPhysicalExaminationArchivesSearchVo personPhysicalExaminationArchivesSearchVo) {
        Person person = personService.selectById(personPhysicalExaminationArchivesSearchVo.getPersonId());
        AssertUtil.notNull(person, "此用户不存在或者已被删除");
        PersonDetailVo personDetailVo = personDetailService.selectVoByPersonId(personPhysicalExaminationArchivesSearchVo.getPersonId());

        return PersonPhysicalExaminationArchivesResultVo.builder()
                .id(personPhysicalExaminationArchivesSearchVo.getPersonId())
                .name(person.getName())
                .sex(Objects.nonNull(personDetailVo) ? personDetailVo.getSex() : null)
                .pageInfo(this.selectPage(personPhysicalExaminationArchivesSearchVo))
                .build();
    }

}